<?php
namespace modules\game\services;

use modules\game\caches\PokerCache;
use modules\game\services\observer\PokerJoiner;
/***
斗地主考虑使用以下设计模式
   观察者   ---------> 
   策略
   职责链 (不合适)
   工厂
   克隆
   状态
***/
class PokerService{

	public static function startPoker(){
		$strategy = new Strategy;
		$person1 = new Person($strategy);//这里注意 内部 strategy 标识符
		$person1->_name = 'Alice';

		$person2 = clone $person1;
		$person2->_name = 'Bob';
			
		$person3 = clone $person1;
		$person3->_name = 'Cinddy';
		//rule生成
		$rule = new Rule;
		$rule->join($person1);
		$rule->join($person2);
		$rule->join($person3);
		$rule->issuePoker();
		//用户第一次出牌生成
		$person1->_isKing  = true;
		$person1->_isPower = true;
		//开始出牌
		//观察者
		$pokerJoiner = new PokerJoiner;
		foreach($rule->_joinPerson as $one){
			echo 'name:' .$one->_name . ' ; poker :' . json_encode($one->_mypoker) . '<br>';
			$pokerJoiner->attach($one);
		}
		echo "begin<br>";
		while(1){
			$pokerJoiner->notify();
			if(PokerJoiner::$_isEnd){
				break;
			}
		}
		echo '===========================<br>';
		echo 'game is over<br>';
		echo 'winner name is ' . Person::$_winner->_name .( Person::$_winner->_isKing ? '(地主胜)' : '(农民胜)'). '<br>';
	}
}